package com.jp.knowledge.my.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jp.knowledge.R;


/**
 * 提示对话框
 *
 * @author gzejia 978862664@qq.com
 */
public class TradeCreateDialog extends Dialog {

    public TradeCreateDialog(Context context) {
        super(context);
    }

    public TradeCreateDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {

        // 上下文
        private Context mContext;
        // 对话框标题
        private String mTitle;
        // 提示内容
        private String mMessage;
        // 取消按钮文本
        private String mPositiveBtnStr;
        // 确认按钮文本
        private String mNegativeBtnStr;
        // 取消按钮监听
        private OnClickListener mPositiveBtnClickListener;
        // 确认按钮监听
        private OnClickListener mNegativeBtnClickListener;

        public Builder(Context mContext) {
            this.mContext = mContext;
        }

        /**
         * @param titleStr 设置标题文本
         */
        public Builder setTitle(String titleStr) {
            this.mTitle = titleStr;
            return this;
        }

        /**
         * @param titleId 设置标题文本
         */
        public Builder setTitle(int titleId) {
            this.mTitle = (String) mContext.getText(titleId);
            return this;
        }

        /**
         * @param positiveBtnStr   设置取消按钮文本
         * @param positiveBtnClick 设置取消按钮点击监听
         */
        public Builder addPositiveBtn(String positiveBtnStr, OnClickListener positiveBtnClick) {
            this.mPositiveBtnStr = positiveBtnStr;
            this.mPositiveBtnClickListener = positiveBtnClick;
            return this;
        }

        /**
         * @param positiveBtnId    设置取消按钮文本
         * @param positiveBtnClick 设置取消按钮点击监听
         */
        public Builder addPositiveBtn(int positiveBtnId, OnClickListener positiveBtnClick) {
            this.mPositiveBtnStr = (String) mContext.getText(positiveBtnId);
            this.mPositiveBtnClickListener = positiveBtnClick;
            return this;
        }

        /**
         * @param negativeBtnStr   设置取消按钮文本
         * @param negativeBtnClick 设置取消按钮点击监听
         */
        public Builder addNegativeBtn(String negativeBtnStr, OnClickListener negativeBtnClick) {
            this.mNegativeBtnStr = negativeBtnStr;
            this.mNegativeBtnClickListener = negativeBtnClick;
            return this;
        }

        /**
         * @param negativeBtnId    设置取消按钮文本
         * @param negativeBtnClick 设置取消按钮点击监听
         */
        public Builder addNegativeBtn(int negativeBtnId, OnClickListener negativeBtnClick) {
            this.mNegativeBtnStr = (String) mContext.getText(negativeBtnId);
            this.mNegativeBtnClickListener = negativeBtnClick;
            return this;
        }

        public String getMessage() {
            return TextUtils.isEmpty(mMessage) ? "" : mMessage;
        }

        /**
         * @return 创建真实对话框
         */
        public TradeCreateDialog create() {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final TradeCreateDialog dialog = new TradeCreateDialog(mContext, R.style.Translucent_NoTitle);
            View layout = inflater.inflate(R.layout.dialog_trade_create, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            View titleParent = layout.findViewById(R.id.dialog_remind_lly_title);
            View btnDividerV = layout.findViewById(R.id.dialog_remind_v_divider);
            TextView titleTv = (TextView) layout.findViewById(R.id.dialog_remind_tv_title);
            final EditText contentEdt = (EditText) layout.findViewById(R.id.dialog_remind_edt_content);
            Button positiveBtn = (Button) layout.findViewById(R.id.dialog_remind_btn_cancel);
            Button negativeBtn = (Button) layout.findViewById(R.id.dialog_remind_btn_sure);

            btnDividerV.setVisibility(View.VISIBLE);
            titleParent.setVisibility(View.VISIBLE);
            positiveBtn.setVisibility(View.VISIBLE);
            negativeBtn.setVisibility(View.VISIBLE);

            if (TextUtils.isEmpty(mTitle)) {
                titleParent.setVisibility(View.GONE);
            } else {
                titleTv.setText(mTitle);
            }

            if (TextUtils.isEmpty(mPositiveBtnStr)) {
                positiveBtn.setVisibility(View.GONE);
                btnDividerV.setVisibility(View.GONE);
            } else {
                positiveBtn.setText(mPositiveBtnStr);
                positiveBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != mPositiveBtnClickListener)
                            mPositiveBtnClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                    }
                });
            }

            if (TextUtils.isEmpty(mNegativeBtnStr)) {
                negativeBtn.setVisibility(View.GONE);
                btnDividerV.setVisibility(View.GONE);
            } else {
                negativeBtn.setText(mNegativeBtnStr);
                negativeBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMessage = contentEdt.getText().toString().trim();
                        if (null != mNegativeBtnClickListener)
                            mNegativeBtnClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                    }
                });
            }

            dialog.setContentView(layout);
            dialog.setCancelable(false);
            return dialog;
        }
    }
}
